<?php 
namespace app\index\controller;
use think\Controller;
class Service extends Controller{
	//首页
	public function index(){
		return $this->fetch();
	}

	//请求验证
	public function sr(){
		if(session('nickname')){
			$this->success('成功');
		}else{
			$this->error('请先登录账号');
		}
	}

	//服务选择
	public function choose(){
		return $this->fetch();
	}

	//服务详情
	public function clean(){
		//已租房屋信息
		$infos = model('Vip')->alias('v')
					->join('Contract c','c.vid=v.vid')
					->join('Renter r','r.rid=c.rid')
					->where(['nickname'=>session('nickname')])->select();
		if($infos != []){
			foreach($infos as $val){
				$info[] = $val->toArray();
			}
			// dump($info);exit;
			$this->assign('info',$info);
		}
		

		foreach(model('ServiceIni')->select() as $val){
			$data[] = $val->toArray();
		}
		// $data_st = $data[0];
		$data_cl = $data[$_GET['type']];
		// $data_rp = $data[2];

		$cl_contents = explode(',',$data_cl['content']);
		foreach($cl_contents as $va){
			$cls[] = explode(':',$va);
		}
		// dump($data_cl);exit;
		$this->assign('data_cl',$data_cl);
		$this->assign('cls',$cls);

		//评价展示
		foreach(model('Service')->alias('s')
				->join('Vip v','v.vid=s.vid and statu=2')
				->select() as $val){
			$comms[] = $val->toArray();
		}
		// dump($comms);exit;
		$this->assign('comms',$comms);

		return $this->fetch();
	}

	//处理clean
	public function do_clean(){
		$vid = model('Vip')->where(['nickname'=>session('nickname')])->find()->vid;
		if(isset($_POST['rid'])){
			$paytime = model('Contract')->where(['rid'=>$_POST['rid'],'vid'=>$vid])->find()->paytime;
			$price = model('Contract')->where(['rid'=>$_POST['rid'],'vid'=>$vid])->find()->price;
			$res[] = round((strtotime($paytime) - time())/24/3600);
			$start=strtotime(date('Y-m-01 00:00:00'));
			$end = strtotime(date('Y-m-d H:i:s'));
			$res[] = 2 - model('Service')->where(['rid'=>$_POST['rid'],'sid'=>2,'statu'=>['neq',3]])->count();
			$res[] = $vid;
			$res[] = $price;

			$this->success($res);
		}
	}

	//提交缴费
	public function do_pay(){
		$vld = $this->validate(input('post.'),'Service');
		if($vld !== true){
			$this->error($vld);
		}
		$_POST['requesttime'] = date('Y-m-d H:i:s',time());
		$this->success($_POST);
	}

	//确认缴费
	public function sure_pay(){
		$_POST['pay'] = $_GET['pay'];
		$res = model('Service')->allowfield(true)->save($_POST);
		if($_POST['sid'] == 1){
			$paytime = model('Contract')->where(['rid'=>$_POST['rid'],'vid'=>$_POST['vid']])->find()->paytime;
			$_POST['paytime'] = date("Y-m-d",strtotime("+1 month",strtotime($paytime)));
			$res2 = model('Contract')->where(['rid'=>$_POST['rid'],'vid'=>$_POST['vid']])->update(['paytime'=>$_POST['paytime']]);
		}
		if($res > 0){
			$this->success('成功');
		}else{
			$this->error($res);
		}
		
	}

}


 ?>